﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Communergy.Common;
using Communergy.Powermeter;
using System.Configuration;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
using CommunergyUploader.Exporters;
using Communergy.Enio;

namespace CurrentCostGUI
{
    public class LocalDataExportCommandRepository : IDataExportCommandRepository
    {
        Communergy.Common.Meter meter;

        public LocalDataExportCommandRepository(Communergy.Common.Meter meter)
        {
            this.meter = meter;
        }

        #region IDataExportCommandRepository Members

        public List<IDataExportCommand> GetDataExportCommands()
        {
            List<IDataExportCommand> dataExportCommands = new List<IDataExportCommand>();
            
            if (meter.MeterGuid != null)
            {
                IDataExportCommand communergyUpload = GetCommunergyUpload();
                dataExportCommands.Add(communergyUpload);
            }

            IDataExportCommand googleUpload = GetGooglePowermeter();
            dataExportCommands.Add(googleUpload);

            IDataExportCommand enioUpload = GetEnioUpload();
            dataExportCommands.Add(enioUpload);

            return dataExportCommands;
        }

        private IDataExportCommand GetEnioUpload()
        {
            string enioApiKey = ConfigurationSettings.AppSettings["EnioAPIKey"];
            var soapDuration = ConfigurationSettings.AppSettings["EnioSoapDuration"];
            TimeSpan timeSpan = SoapDuration.Parse(soapDuration);
            IDataExportCommand enioUpload = new EnioExportCommand(meter.MeterId.ToString(), timeSpan, enioApiKey);
            enioUpload.ExportInterval = timeSpan;
            return enioUpload;
        }

        private IDataExportCommand GetCommunergyUpload()
        {
            IDataExportCommand communergyUpload = new CommunergyServerExportCommand(meter);
            var communergySoapDuration = ConfigurationSettings.AppSettings["CommunergySoapDuration"];
            TimeSpan interval = SoapDuration.Parse(communergySoapDuration);
            communergyUpload.ExportInterval = interval;
            return communergyUpload;
        }

        private IDataExportCommand GetGooglePowermeter()
        {


            PowermeterData powerMeterData = new PowermeterData();
            powerMeterData.AuthToken = ConfigurationSettings.AppSettings["AuthToken"];
            powerMeterData.DeviceName = ConfigurationSettings.AppSettings["DeviceName"];
            powerMeterData.PowermeterBaseUrl = ConfigurationSettings.AppSettings["PowermeterBaseUrl"];
            powerMeterData.SecurityZone = ConfigurationSettings.AppSettings["GoogleUserId"];
            powerMeterData.UserId = ConfigurationSettings.AppSettings["GoogleUserId"];
            powerMeterData.Measurement = ConfigurationSettings.AppSettings["GoogleMeasurement"];

            var googleSoapDuration = ConfigurationSettings.AppSettings["GoogleSoapDuration"];
            TimeSpan googleTimeSpan = SoapDuration.Parse(googleSoapDuration);

            IDataExportCommand googleUpload = new GooglePowermeterExportCommand(meter.MeterId.ToString(), googleTimeSpan, powerMeterData);

            return googleUpload;
        }

        #endregion
    }
}
